package cn.dujc.superkid.activities;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.dujc.core.ui.BaseActivity;
import cn.dujc.core.util.ToastUtil;
import cn.dujc.superkid.R;
import cn.dujc.superkid.api.HttpHelper;
import cn.dujc.superkid.api.QMap;
import cn.dujc.superkid.api.Result;
import cn.dujc.superkid.api.ResultCallBack;
import cn.dujc.superkid.bean.RegisterResult;
import cn.dujc.superkid.configs.Globals;
import cn.dujc.superkid.utils.CountDownUtil;

public class RegisterActivity extends BaseActivity implements View.OnClickListener{
    private final String TAG = RegisterActivity.class.getName();
    private Button btn_register;
    private TextView send_code;
    private TextView et_phone;
    private TextView code_text;
    private TextView et_password;
    private TextView register;
    private String  codephone;
    private String codekey;

    @Override
    public int getViewId() {
        return R.layout.activity_register;
    }

    @Nullable
    @Override
    public Toolbar initToolbar(ViewGroup parent) {
        final Toolbar toolbar = (Toolbar) LayoutInflater.from(parent.getContext()).inflate(R.layout.toolbar_register, parent, false);
        toolbar.findViewById(R.id.toolbar_back_id).setOnClickListener(this);
        TextView title = toolbar.findViewById(R.id.toolbar_title_id);
        title.setText("注册");
        return toolbar;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        initView();
    }
    private void initView(){
        btn_register = findViewById(R.id.btn_register);
        send_code = findViewById(R.id.send_code);
        register = findViewById(R.id.register);
        et_phone = findViewById(R.id.et_phone);
        code_text = findViewById(R.id.code_text);
        et_password=findViewById(R.id.et_password);
        send_code.setOnClickListener(this);
        btn_register.setOnClickListener(this);
        register.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        final String phone = et_phone.getText().toString();
        final String password = et_password.getText().toString();
        final String codetext = code_text.getText().toString();
        switch (v.getId()){
            case R.id.send_code:
                if (!phoneCheck(Globals.PHONE_CHECK,phone)){
                    if (phone.equals("")){
                        ToastUtil.showToast(mActivity, "请输入手机号");
                    }else {
                        ToastUtil.showToast(mActivity, "请输入正确的手机号");
                    }
                }
                else {
                    HttpHelper.getLoginRegister().getCode(QMap.build()
                            .put("username", phone).put("is_exist", "1")
                            .create()).enqueue(new ResultCallBack<Result<String>>(mActivity) {
                        @Override
                        public void onSuccessCallback(int code, Result<String> body) {
                            if (body != null) {
                                ToastUtil.showToast(mActivity, body.getMsg());
                                codekey=body.getData();
                                new CountDownUtil(send_code)
                                        .setCountDownMillis(60_000L)//倒计时60000ms
                                        .setCountDownColor(android.R.color.holo_blue_light,android.R.color.darker_gray)//不同状态字体颜色
                                        .setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                            }
                                        })
                                        .start();
                            }
                        }
                    });
                }
                break;
            case R.id.btn_register:
                if (!phoneCheck(Globals.PHONE_CHECK,phone)){
                    if (phone.equals(null)){
                        ToastUtil.showToast(mActivity, "请输入手机号");
                    }else {
                        ToastUtil.showToast(mActivity, "请输入正确的手机号");
                    }
                }
                else if (code_text==null) {
                    ToastUtil.showToast(mActivity, "验证码不能为空");
                }
                else {
                    Log.i(TAG,"==============caizhijia======="+codekey );
                    Log.i(TAG,"==============caizhijia======="+codetext );
                    Log.i(TAG,"==============caizhijia======="+phone );
                    Log.i(TAG,"==============caizhijia======="+password );
                    HttpHelper.getLoginRegister().userRegister(QMap.build()
                            .put("username", phone)
                            .put("password", password)
                            .put("code", codetext)
                            .put("codekey", codekey)
                            .create()).enqueue(new ResultCallBack<Result<RegisterResult>>(mActivity) {
                        @Override
                        public void onSuccessCallback(int code, Result<RegisterResult> body) {
                            if (body != null) {
                                finish();
                            }

                        }
                    });
                }
                break;
            case R.id.register:
                finish();
            case R.id.toolbar_back_id:
                finish();
            default:
                break;
        }
    }
    public static boolean phoneCheck(String patttern, String username){
        Pattern pattern = Pattern.compile(patttern);
        Matcher matcher = pattern.matcher(username);
        if(matcher.find()){
            return  true;
        }
        else {
            return false;
        }
    }
}
